/**
 * 满减券栏目构造js
 * Created by sumi_EC on 2018/03/07.
 */
// <link rel="stylesheet" href="../css/coupon.css">
/*
 <div class="couponContainer uhide" url-value="account/couponDetail.html">
 <div class="couponBorder">
 <div class="couponBorderFrame"></div>
 </div>
 </div>
 */
// <script src="../js/coupon.js"></script>
/**
 * 鼠标滑轮控制满减券栏位水平滚动事件
 */
function bindMouseWheel($dom) {
    var MouseWheel = function (e) {
        e = e || window.event;
        if (e.stopPropagation) {
            e.stopPropagation();
        } else {
            e.cancelBubble = true;
        }
        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
        if (firefox) {
            if (e.detail < 0) {
                $dom.scrollLeft($dom.scrollLeft() - 100);
            } else {
                $dom.scrollLeft($dom.scrollLeft() + 100);
            }
        } else {
            if (e.wheelDelta > 0) {
                $dom.scrollLeft($dom.scrollLeft() - 100);
            } else {
                $dom.scrollLeft($dom.scrollLeft() + 100);
            }
        }
    };
    if ($dom[0]) {
        var firefox = navigator.userAgent.indexOf('Firefox') !== -1;
        firefox ? $dom[0].addEventListener('DOMMouseScroll', MouseWheel, false) : ($dom[0].onmousewheel = MouseWheel);
    }
}
/**
 * 获取满减数据
 */
function getCouponData() {
    setTimeout(function () {
      // if (getLocVal('zanshipingbimanjianquan') !== "true") return false; //暂时屏蔽满减券
        $(".couponContainer").addClass("uhide");//退出登录时候隐藏
        if (sessionStorage.getItem('loginflag') !== "1") return false;
        var coupFundcode = "";
        if(sessionStorage.getItem("coupFundcode")){
            coupFundcode = sessionStorage.getItem("coupFundcode");
            sessionStorage.removeItem("coupFundcode"); 
        }
        nfApp._ajax({
            "function": "COUP001",
            "sessionkey": Decrypt(getLocVal('sessionkey')),
            "fundcode":coupFundcode,
            "channel": "mweb"
        }, function (data) {
            console.info("满减券数据: " + data.list, data);
            if (data.code == SUCCESS_FLAG) {
                //延时为了页面渲染完之后再构造卡片，防止界面样式混乱
                /*
                 amount: 2
                 balance : 30
                 couponId : "116"
                 couponOperationId : "203"
                 desp : ""
                 expireDay : 2
                 fundCodeList  : Array[0]
                 fundInfoList : Array[0]
                 fundNameList : Array[0]
                 minThreshold : 1000
                 score : 9
                 */
                setLocVal("AllCouponData", json2str(data.list));
                makeCouponCard(data.list);
            }
        });
    }, 300);
}
/**
 * 构造减免卷uexGestureUnlock
 */
function makeCouponCard(data) {
    if (data.length < 1) return;
    var $couponContainer = $('.couponContainer'),
        $couponBorderFrame = $('.couponBorderFrame'),
        $couponBorder = $('.couponBorder');
    var useRange, newCard, newLeftCard,
        newRightCard, newCenterCard, newCenterTop, newCenterCenter, newCenterBottom, totalWidth = 0;
    $couponBorderFrame.children().remove();//清除数据
    $couponContainer.removeClass("uhide");
    for (var num = 0; num < $couponContainer.length; num++) {
        if ($couponContainer[num].offsetWidth) {
            //防止其它界面未消除的元素影响
            $couponContainer = $($couponContainer[num]);
            $couponBorderFrame = $($couponBorderFrame[num]);
            $couponBorder = $($couponBorder[num]);
        }
    }
    var symbolDom = '<div class="couponSymbol">¥&nbsp;</div>';
    var $hotList2 = $('#hotList2'), hideFlag = false;
    if ($hotList2.length && $hotList2.hasClass("uhide")) {
        //热门列表隐藏会导致卡片样式混乱 即产品也选择全部基金后在登录
        $hotList2.removeClass("uhide");
        hideFlag = true;
    }
    for (var i = 0; i < data.length; i++) {
        //卡片样式添加
        newCard = document.createElement('div');
        newLeftCard = document.createElement('div');
        newCenterTop = document.createElement('div');
        newCenterCenter = document.createElement('div');
        newCenterBottom = document.createElement('div');
        newCenterCard = document.createElement('div');
        newRightCard = document.createElement('div');
        newCard.classList.add('couponCard');
        newCard.classList.add('couponMargin');
        newLeftCard.classList.add('couponLeftCard');
        newCenterTop.classList.add('couponCenterTop');
        newCenterCenter.classList.add('couponCenterCenter');
        newCenterBottom.classList.add('couponCenterBottom');
        newCenterCard.classList.add('couponCenterCard');
        newCenterCard.appendChild(newCenterTop);
        newCenterCard.appendChild(newCenterCenter);
        newCenterCard.appendChild(newCenterBottom);
        newRightCard.classList.add('couponRightCard');
        if (i === 0) {
            newCard.classList.add('couponNML');
        } else if (i === length - 1) {
            newCard.classList.add('couponNMR');
        }
        newCard.appendChild(newLeftCard);
        newCard.appendChild(newCenterCard);
        newCard.appendChild(newRightCard);
        //卡片内容添加
        if (data[i].fundInfoList.length == 0) {
            useRange = "全部产品"
        } else {
            useRange = "指定产品"
        }
        newLeftCard.innerHTML = symbolDom + data[i].balance;
        newRightCard.innerHTML = '满' + data[i].minThreshold + '元可用<br>' + useRange;
        newCard.setAttribute('data-value', json2str(data[i]));
        //卡片方法添加
        newCard.onclick = function () {
            setLocVal("selectedCouponData", this.getAttribute("data-value"));
            location.href = $couponContainer[0].getAttribute("url-value");
        };
        $couponBorderFrame.append(newCard);
        newCenterTop.style.width = newCenterTop.offsetHeight + "px";
        newCenterBottom.style.width = newCenterBottom.offsetHeight + "px";
        newCenterCenter.style.right = newCenterBottom.offsetHeight / 2.5 + "px";
        totalWidth += newCard.offsetWidth;
    }
    newCard = document.createElement('div');
    newCard.setAttribute("style", "clear:both;");//清除$couponBorderFrame中的浮动，防止高度塌陷
    $couponBorderFrame.append(newCard);
    $couponBorderFrame.width(i * 16 + totalWidth);
    $couponBorderFrame.css("min-width", $couponBorder.width() - 2 * parseFloat($couponBorderFrame.css("padding")));//最小为屏幕宽度  防止宽度坍塌
  
    if (hideFlag) {
        //热门列表隐藏会导致卡片样式混乱 即产品也选择全部基金后在登录
        $hotList2.addClass("uhide");
    }
    //     隐藏水平滚动条
    /*
     if (!$couponContainer.hasClass("oy-hidden")) {
     $couponContainer.height($couponContainer.height() - 15);
     $couponContainer.addClass("oy-hidden");
     }
     */
    bindMouseWheel($couponBorder);
}